package com.chuanyang.driverh.mainstart.fragment;


import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuanyang.driverh.BaseApplication;
import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseFragment;
import com.chuanyang.driverh.mainmy.AccredCarOrShipTeamActivity;
import com.chuanyang.driverh.mainmy.LinesManageActivity;
import com.chuanyang.driverh.mainmy.MyCarHadAccreditationActivity;
import com.chuanyang.driverh.mainmy.MyCarTeamActivity;
import com.chuanyang.driverh.mainmy.MyCarTeamHasActivity;
import com.chuanyang.driverh.mainmy.MyShipHadAccreditationActivity;
import com.chuanyang.driverh.mainmy.OrderHistoryActivity;
import com.chuanyang.driverh.mainmy.PassidentifyActivity;
import com.chuanyang.driverh.mainmy.SettingActivity;
import com.chuanyang.driverh.mainmy.SubscriberActivity;
import com.chuanyang.driverh.mainmy.UserInfoActivity;
import com.chuanyang.driverh.mainother.LookPhotoDetailActivity;
import com.chuanyang.driverh.responseresult.DriverInfoData;
import com.chuanyang.driverh.utils.BitmapUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.eventbus.RefleshDriverInfoEvent;

/**
 * 个人中心
 */
public class PersonalCenterFragment extends BaseFragment {
    private ImageView iv_drivericon;
    private TextView tv_name;
    private TextView tv_phone;
    private LinearLayout carteam;
    public LinearLayout carmamage;
    private LinearLayout userinfo;
    private LinearLayout ll_personalcenter_credit;
    private LinearLayout ll_personalcenter_wallet;
    private LinearLayout ll_personalcenter_subscribe;
    private LinearLayout ll_personalcenter_lookhistory;
    private LinearLayout ll_personalcenter_lines;
    private LinearLayout ll_personalcenter_setting;
    private LinearLayout ll_personalcenter_opreateguide;
    private ImageView iv_personcenter_translatestate;
    private TextView tv_personal_drivertype;
    private TextView tv_personal_plantnumber;

    private DriverInfoData driverinfo;

    private int clickFlag;//是否点击了车船队管理。0、未点击。1、点击了车队管理，2，点击了船队管理。

    @Override
    protected int getLayoutResId() {
        return R.layout.my_frag_personalcenter;
    }

    @Override
    protected void initView(View view) {
        iv_drivericon = (ImageView) rootView.findViewById(R.id.iv_personcenter_usericon);
        tv_name = (TextView) rootView.findViewById(R.id.tv_personcenter_username);
        tv_phone = (TextView) rootView.findViewById(R.id.tv_personal_phone);
        tv_personal_drivertype = (TextView) rootView.findViewById(R.id.tv_personal_drivertype);//显示司机还是车队长 tv_personal_drivertype
        iv_personcenter_translatestate = (ImageView) rootView.findViewById(R.id.iv_personcenter_translatestate);
        tv_personal_plantnumber = (TextView) rootView.findViewById(R.id.tv_personal_plantnumber);
        //车辆管理
        carteam = (LinearLayout) rootView.findViewById(R.id.ll_personalcenter_carship);
        //车队管理
        carmamage = (LinearLayout) rootView.findViewById(R.id.ll_personalcenter_carshipteam);
        //个人信息
        userinfo = (LinearLayout) rootView.findViewById(R.id.ll_personcenter_userinfo);
        //积分
        ll_personalcenter_credit = (LinearLayout) rootView.findViewById(R.id.ll_personalcenter_credit);
        //钱包
        ll_personalcenter_wallet = (LinearLayout) rootView.findViewById(R.id.ll_personalcenter_wallet);
        //订阅
        ll_personalcenter_subscribe = (LinearLayout) rootView.findViewById(R.id.ll_personalcenter_subscribe);
        //查看历史
        ll_personalcenter_lookhistory = (LinearLayout) rootView.findViewById(R.id.ll_personalcenter_lookhistory);
        //路线管理
        ll_personalcenter_lines = (LinearLayout) rootView.findViewById(R.id.ll_personalcenter_lines);
        //基本设置
        ll_personalcenter_setting = (LinearLayout) rootView.findViewById(R.id.ll_personalcenter_setting);
        //操作指南
        ll_personalcenter_opreateguide = (LinearLayout) rootView.findViewById(R.id.ll_personalcenter_opreateguide);

    }

    @Override
    protected void initData() {
        driverinfo = BaseApplication.getInstance().getDriverInfo();
        if (driverinfo == null) {
            return;
        }
        if (!StringUtil.isBlank(driverinfo.getUserImage())) {
            BitmapUtil.showImgForNetPath(driverinfo.getUserImage(), iv_drivericon);
        }

        if (!StringUtil.isBlank(driverinfo.getPhone())) {
            tv_phone.setText(StringUtil.handlePhoneNumShow(driverinfo.getPhone()));
        }
        if (!StringUtil.isBlank(driverinfo.getUsername())) {
            tv_name.setText(driverinfo.getUsername());
        }

        if (driverinfo.getDriver() != null) {//车辆信息有,设置状态。
            if (driverinfo.getDriver().getTransportState() == null) {
                iv_personcenter_translatestate.setBackgroundResource(R.mipmap.free);
            } else {
                if (driverinfo.getDriver().getTransportState() == 1) {
                    iv_personcenter_translatestate.setBackgroundResource(R.mipmap.transport);
                } else {
                    iv_personcenter_translatestate.setBackgroundResource(R.mipmap.free);
                }
            }

            if (driverinfo.getDriver().getPlateNumber() != null) {//车牌船号有
                tv_personal_plantnumber.setText(driverinfo.getDriver().getPlateNumber());
            }
        }

        if (driverinfo.getFleet() != null) {//有车队信息，设置队长队员
            if (driverinfo.getFleet().getFleetLeader().equals(driverinfo.getDriverId())) {
                tv_personal_drivertype.setText("队长");
            } else {
                tv_personal_drivertype.setText("队员");
            }
        }


    }

    @Override
    protected void initListener() {
        carteam.setOnClickListener(this);
        carmamage.setOnClickListener(this);
        userinfo.setOnClickListener(this);
        ll_personalcenter_credit.setOnClickListener(this);
        ll_personalcenter_wallet.setOnClickListener(this);
        ll_personalcenter_subscribe.setOnClickListener(this);
        ll_personalcenter_lookhistory.setOnClickListener(this);
        ll_personalcenter_lines.setOnClickListener(this);
        ll_personalcenter_setting.setOnClickListener(this);
        ll_personalcenter_opreateguide.setOnClickListener(this);
        iv_drivericon.setOnClickListener(this);
    }
/*
    @Override
    public void visibleToUser() {
            initData();
    }*/

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_personcenter_usericon:
                if (driverinfo != null && !StringUtil.isBlank(driverinfo.getUserImage())) {
                    startActivity(LookPhotoDetailActivity.class, driverinfo.getUserImage());
                }
                break;
            case R.id.ll_personcenter_userinfo:
                startActivity(UserInfoActivity.class);
                break;
            case R.id.ll_personalcenter_credit://积分
                // startActivity(AccountJifenActivity.class);
                PrintUtil.toast(getActivity(), "此功能尚未开通!");
                break;
            case R.id.ll_personalcenter_wallet://钱包
                PrintUtil.toast(getActivity(), "此功能尚未开通!");
//                startActivity(WalletActivity.class);
                break;
            case R.id.ll_personalcenter_subscribe:
                startActivity(SubscriberActivity.class);  //订阅
                break;
            case R.id.ll_personalcenter_carship://此处有车辆未认证和已认证两个页面，跳转时需要判断
                clickFlag=1;
                BaseApplication.getInstance().getDriverInfoFromNet();
                break;
            case R.id.ll_personalcenter_carshipteam://此处有车队未创建和已创建两个页面，跳转时需要判断（由于后台没有给出过渡状态，所以无法判断是否在创建中）
                clickFlag=2;
                BaseApplication.getInstance().getDriverInfoFromNet();
                break;
            case R.id.ll_personalcenter_lookhistory:
                startActivity(OrderHistoryActivity.class);
                break;
            case R.id.ll_personalcenter_lines:
                // startActivity(PathManageActivity.class);
                //如果个人中心进入 "1"     如果从添加路线进入
                startActivity(LinesManageActivity.class, "1");
                break;
            case R.id.ll_personalcenter_setting:
                startActivity(SettingActivity.class);
                break;
            case R.id.ll_personalcenter_opreateguide:
                PrintUtil.toast(getActivity(), "此功能尚未开通!");
                // startActivity(UserInfoActivity.class);
//                startActivity(CarPersonIdentifyActivity.class);
                break;
        }

    }


    private void identifyCarOrShip() {//点击车船管理
        if (driverinfo.getDriver() == null) {//车和船都未认证
            if(StringUtil.isBlank(driverinfo.getiDNumber())){
                PrintUtil.toast(getActivity(),"请先完善个人信息");
            }else {
                startActivity(AccredCarOrShipTeamActivity.class);
            }
        } else {
            if (driverinfo.getDriver().getDriverState() == 1) {//此处表示已认证
                if (driverinfo.getDriver().getDriverType() == 2) {//车辆
                    startActivity(MyCarHadAccreditationActivity.class, "1");//车辆已认证
                } else if (driverinfo.getDriver().getDriverType() == 3) {//船只
                    startActivity(MyShipHadAccreditationActivity.class);//船只已认证
                }
            } else if (driverinfo.getDriver().getDriverState() == 2) {//表示在认证中
                startActivity(PassidentifyActivity.class, "1");
            } else if (driverinfo.getDriver().getDriverState() == 3) {//表示删除认证信息
                startActivity(AccredCarOrShipTeamActivity.class);
            }
        }

    }

    private void identifyCarOrShipTeam() {
        if (driverinfo.getFleet() != null && driverinfo.getFleet().getFleetLeader() != null){//此处无法直接通过车队类型来判断 ，要通过driverType来判断
            if (driverinfo.getDriver().getDriverType() == 2){//车队
                if (driverinfo.getFleet().getFleetState() == 1) {//车队已认证
                    startActivity(MyCarTeamHasActivity.class, "1");//车队已
                } else {//车队认证中
                    startActivity(PassidentifyActivity.class, "2");
                }
            } else if (driverinfo.getDriver().getDriverType() == 3) {//船队
                if (driverinfo.getFleet().getFleetState() == 1){//船队已认证
                    startActivity(MyCarTeamHasActivity.class, "2");
                } else {//船队认证中
                    startActivity(PassidentifyActivity.class, "3");
                }
            }
        } else {////车队和船队未创建时
            if(StringUtil.isBlank(driverinfo.getiDNumber())){
                PrintUtil.toast(getActivity(),"请先完善个人信息");
            } else if(driverinfo.getDriver()==null){
                PrintUtil.toast(getActivity(),"请先进行车船信息认证");
            } else if (driverinfo.getDriver().getDriverState() != null && driverinfo.getDriver().getDriverState() == 2) {
                PrintUtil.toast(getActivity(),"您车或船未认证通过，无法创建队伍！");
            }else{
                startActivity(MyCarTeamActivity.class);//车队和船队未创建时
            }
        }
    }


    public void onEventMainThread(RefleshDriverInfoEvent event) {
        if (event.isRefleshDriverInfo()){
            initData();
            if (clickFlag==1){
                clickFlag=0;
                identifyCarOrShip();
            }else if (clickFlag==2){
                clickFlag=0;
                identifyCarOrShipTeam();
            }

        }

        if (driverinfo!=null &&driverinfo.getFleet()!=null && driverinfo.getFleet().getFleetLeader().equals(driverinfo.getDriverId())){
            SharedPrefUtil.setFleetId(driverinfo.getFleet().getId()+"");
        }else{
            SharedPrefUtil.setFleetId("");
        }


        /*if (driverinfo!=null &&driverinfo.getFleet()!=null && driverinfo.getFleet().getFleetLeader()){

        }*/

    }





  /*  private void getCarTeamDetailInfo(String fleetId) {
        DialogUtil.showLoadingDialog(activityContext,null);
        Map<String,String> params=new HashMap<String, String>();
        params.put("fleetId", fleetId);
        MyHttpUtil.selectFleetInfo(activityContext,params, new MyCallback<CarTeamDetailResult>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(CarTeamDetailResult result, int id) {
                if (result.getCode() == ConstUtil.OK  && result.getData() != null) {

                }else{
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }
        });

    }*/


}